#pragma once
#include <iostream>
#include "user.hpp"
#include "option.hpp"
#include <string.h>

using namespace std;

enum Mark
{
    ADD,
    DELETE,
    UPDATE,
    DISPLAY,
    EXIT,
    ERR
};

class Manager
{
private:
    User *user;
    Option *option = new Sql;

public:
    void run();
    void menu();
    void my_add();
    void my_delete();
    void my_update();
    void my_display();
    int change(string &s);
};
void Manager::run()
{
    Manager action;
    option->init();
    action.menu();

    string sign;

    while (1)
    {
        cin >> sign;
        switch(change(sign))
        {
            case ADD:
            {
                action.my_add();
                cout << '\n' << "Contact addition completed!\n";
                cout << "Press any key to continue!\n";
                getchar();
                break;
            }
            case DELETE:
            {
                action.my_delete();
                cout << '\n' << "Contact deletion completed!\n";
                cout << "Press any key to continue!\n";
                getchar();
                break;
            }
            case UPDATE:
            {
                action.my_update();
                cout << '\n' << "Contact update completed!\n";
                cout << "Press any key to continue!\n";
                getchar();
                break;
            }
            case DISPLAY:
            {
                action.my_display();
                cout << '\n' << "Contact display completed!\n";
                cout << "Press any key to continue!\n";
                getchar();
                break;
            }
            case EXIT:
            {
                cout << "Program has exited!\n";
                exit(0);
            }
            case ERR:
            {
                cout << "Please enter the specified type!\n";
                cout << "Press any key to continue!\n";
                getchar();
                break;
            }
            default:
            {
                break;
            }
        }
    }
}
void Manager::menu()
{
    system("clear");
    printf("\033[1;33;45m\t+========================================+\033[0m\n");
    printf("\033[1;33;45m\t|                                        |\033[0m\n");
    printf("\033[1;33;45m\t|            添加信息: ADD               |\033[0m\n");
    printf("\033[1;33;45m\t|            查看信息: SEE               |\033[0m\n");
    printf("\033[1;33;45m\t|            搜索信息: SEARCH            |\033[0m\n");
    printf("\033[1;33;45m\t|            删除信息: DELETE            |\033[0m\n");
    printf("\033[1;33;45m\t|            更新信息: UPDATE            |\033[0m\n");
    printf("\033[1;33;45m\t|            保存退出: EXIT              |\033[0m\n");
    printf("\033[1;33;45m\t|                                        |\033[0m\n");
    printf("\033[1;33;45m\t+========================================+\033[0m\n");
    printf("\033[1;33;45m\t请输入要进行的操作: ");
}
void Manager::my_add()
{
    cout << "Please enter the name of the contact to be added : ";
    cin >> user->name;
    cout << "Please enter the age of the contact you want to add: ";
    cin >> user->age;
    cout << "Please enter the phone number of the contact to be added: ";
    cin >> user->tel;
    cout << "Please enter the address of the contact to be added: ";
    cin >> user->addr;

    option->insert_info(*(user));
}
void Manager::my_delete()
{
    //memset(user, 0, sizeof(User));
    string name;
    cout << "Please Enter the name of the contact you want to delete: ";
    cin >> name;
    option->delete_info(name);
}
void Manager::my_update()
{
    string name;
    cout << "Please enter the name of the want to updated : ";
    cin >> name;
    cout << "Please enter the name of the contact to be updated: ";
    cin >> user->name;
    cout << "Please enter the age of the contact you want to updated: ";
    cin >> user->age;
    cout << "Please enter the phone number of the contact to be updated: ";
    cin >> user->tel;
    cout << "Please enter the address of the contact to be updated: ";
    cin >> user->addr;

    if(option->find(name))
    {
        option->update_info(*user, name);
    }
    else
    {
        cout << "No such person, please retype!\n";
    }
    
}
void Manager::my_display()
{
    option->display();
}
int Manager::change(string &s)
{
    if(s == "ADD"||s == "add")
    {
        return ADD;
    }
    else if(s == "DELETE"||s == "delete")
    {
        return DELETE;
    }
    else if(s == "UPDATE"||s == "update")
    {
        return UPDATE;
    }
    else if(s == "DISPLAY" || s == "display")
    {
        return DISPLAY;
    }
    else if(s == "EXIT" || s == "exit")
    {
        return EXIT;
    }
    else
    {
        return ERR;
    }
}
